﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace NodeGardenSl.ViewModels
{
    public class ViewModel
    {

        private const int CircleCountMax = 15;

        public ObservableCollection<Item> Items { get; set; }
        public ObservableCollection<Item> OutputItems { get; set; }
        public readonly Queue<Item> data;
        public readonly Queue<Item> outputData;
        public string TwitterName { get; set; }
       
        public ViewModel(string twitterName)
        {
            this.TwitterName = twitterName;
            Items = new ObservableCollection<Item>();
            OutputItems = new ObservableCollection<Item>();
            data = new Queue<Item>();
            outputData = new Queue<Item>();
            AddMessage(". . . . . . . . . . . " + twitterName);
       }

        public void AddMessage(string message)
        {
            string[] words = message.Split(' ');
            foreach (var word in words)
            {
                AddData(word);
            }
        }


        public bool HasData()
        {
            return this.data.Count > 0;
        }

        public void AddData(string value)
        {
            data.Enqueue(new Item { Name = value });
        }

        public void AddData(Item item)
        {
       
            System.Diagnostics.Debug.WriteLine("Adding Data: " + item.Name);
            data.Enqueue(item);

        }

        public void RemoveItem(object parameter)
        {
            Items.RemoveAt(Items.Count - 1);
        }

        internal void Next()
        {
            
            if (data.Count > 0)
            {
                if (Items.Count > CircleCountMax)
                {
                    Item toRemove = Items[0];
                    EnqueOutput(toRemove);
                    Items.RemoveAt(0);
                }
                Items.Add(data.Dequeue());
            }

            
            if (this.outputData.Count > 0)
            {
                if (OutputItems.Count > 5)
                {
                    OutputItems.RemoveAt(0);
                }
                OutputItems.Add(outputData.Dequeue());
            }


            if (data.Count == 0)
            {
                AddMessage(". . . . . . . . . ");
            }
        }

        public void EnqueOutput(Item newItem)
        {
            this.outputData.Enqueue(newItem);
        }


       
               

    }

    
}
